import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  return {
    plugins: [vue()],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
      },
    },
    server: {
      port: 3000,
      open: true,
      proxy: {
        [env.VITE_API_BASE_URL]: {
          target: mode === 'production'
            ? 'https://your-production-api.com'
            : mode === 'test'
            ? 'https://your-test-api.com'
            : 'http://localhost:8080',
          changeOrigin: true,
          // 重写请求路径，把前缀（如 /api）去掉，只保留后面的路径。
          rewrite: (path) => path.replace(new RegExp(`^${env.VITE_API_BASE_URL}`), ''),
        },
      },
    },
  }
})